/*
	Azid Engine is 3D Game Engine developed along side with Galactic Siege (see Google Code) 
	Copyright (C) 2010  Jorgen Ader

	This file is part of AzidEngine.

	AzidEngine is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	AzidEngine is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with AzidEngine.  If not, see <http://www.gnu.org/licenses/>. 
*/

#include "../includes/AzidEngine.hpp"
#include "../includes/EngineStartUpState.hpp"
#include "../includes/EngineMenuState.hpp"
#include "../includes/EngineGameState.hpp"
#include <sstream>

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, char *CmdLine, INT) 
{
	int argc;
	char **argv;
	char*  arg;
    int    index;
    argc = 1;
    arg  = CmdLine;
    
    while (arg[0] != 0) {

        while (arg[0] != 0 && arg[0] == ' ') {
            arg++;
        }

        if (arg[0] != 0) {
        
            argc++;
        
            while (arg[0] != 0 && arg[0] != ' ') {
                arg++;
            }
        
        }
    
    }    

	argv = (char**)malloc(argc * sizeof(char*));
    arg = CmdLine;
    index = 1;

    while (arg[0] != 0) {

        while (arg[0] != 0 && arg[0] == ' ') {
            arg++;
        }

        if (arg[0] != 0) {
        
            argv[index] = arg;
            index++;
        
            while (arg[0] != 0 && arg[0] != ' ') {
                arg++;
            }
        
            if (arg[0] != 0) {
                arg[0] = 0;    
                arg++;
            }
        
        }
    
    }    

    char filename[_MAX_PATH];
    GetModuleFileNameA(NULL, filename, _MAX_PATH);
    argv[0] = filename;

#else
int main(int argc, char *argv[])
{
#endif
	int height = 0, width = 0, port;

	for(int i=1;i<argc;i++)
	{
		if(std::string(argv[i]) == "-w" || std::string(argv[i]) == "--width")
		{
			std::stringstream ss;
			ss << argv[i+1];
			ss >> width;
			if(width < 640)
				width = 0;
			i++;
		}
		else if(std::string(argv[i]) == "-h" || std::string(argv[i]) == "--height")
		{
			std::stringstream ss;
			ss << argv[i+1];
			ss >> height;
			if(height < 480)
				height = 0;
			i++;
		}
		else if(std::string(argv[i]) == "-p" || std::string(argv[i]) == "--port")
		{
			std::stringstream ss;
			ss << argv[i+1];
			ss >> port;
			if(port < 7400)
				port = 7400;
			i++;
		}
	}
	try
	{
		AzidEngine::AzidGame::create();
		AzidEngine::AzidGame::getSingletonPtr()->initEngine("GameApp", height, width);
		AzidEngine::StartUpState* startState = new AzidEngine::StartUpState;
		AzidEngine::MenuState* menuState = new AzidEngine::MenuState;
		AzidEngine::GameState* gameState = new AzidEngine::GameState;
		AzidEngine::AzidGame::getSingletonPtr()->StateMgr->addState(startState);
		AzidEngine::AzidGame::getSingletonPtr()->StateMgr->addState(menuState);
		AzidEngine::AzidGame::getSingletonPtr()->StateMgr->addState(gameState);
		AzidEngine::AzidGame::getSingletonPtr()->startGame();
		AzidEngine::AzidGame::destroy();
    }
    catch (Ogre::Exception& e) 
	{
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32 
        MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
        std::cerr << "Exception: ";
        std::cerr << e.getFullDescription().c_str() << "\n";
#endif
	return 1;
	}
	return 0;
}
